<?php
class Authentication
{

	/**
	 * Session Entity
	 *
	 * @var sessionEntity object
	 */
	protected $sessionEntity = null;

	/**
	 * CustomerOrderSessionEntity Entity
	 *
	 * @var CustomerOrderSessionEntity object
	 */
	protected $customerOrderSessionEntity = null;

	// Role list
	protected $roleList	= array(ROLE_ADMIN_SUPER, ROLE_MEMBER, ROLE_SHIPPING, ROLE_DEALMANAGER, ROLE_ACCOUNTANT, ROLE_SUPPORT);

	// Status list
	protected $statusList = array(USER_STATUS_INACTIVE, USER_STATUS_ACTIVE, USER_STATUS_SUSPENDED, USER_STATUS_BLOCKED, USER_STATUS_DELETED);

	/**
	 * Authenticate contructor
	 * If not specified user entity, user entity currently logged in should be used
	 * If specified user entity, this user entity should be used
	 *
	 * @param UserEntity $userEntity: option
	 */
	public function __construct($userEntity = null)
	{
		// If not specified user entity, user entity currently logged in should be used

		if (is_null($userEntity)){
			if (isset($_SESSION[SESSION_USER_LOGGED]) && !is_null($_SESSION[SESSION_USER_LOGGED])){
				$this->sessionEntity = $_SESSION[SESSION_USER_LOGGED];
			}
		}else{

			// Specified user entity, this user entity should be used
			$this->sessionEntity = new SessionEntity($userEntity);
		}

		//Init CustomerOrderSessionEntity
//		if (isset($_SESSION[SESSION_CUSTOMER_ORDER]) && !is_null($_SESSION[SESSION_CUSTOMER_ORDER])){
//			$this->customerOrderSessionEntity = $_SESSION[SESSION_CUSTOMER_ORDER];
//		}
//		else
//		{
//			$this->customerOrderSessionEntity = new CustomerOrderSessionEntity();
//			$_SESSION[SESSION_CUSTOMER_ORDER] = $this->customerOrderSessionEntity;
//		}
	}

	/**
	 * Validate status of user
	 *
	 * @param int $status: option
	 *
	 * @return bool
	 */
	public function validateStatus($status = null)
	{
		// Check session entity
		if (is_null($this->sessionEntity)){
			return false;
		}

		// Validate status of user
		if (is_null($this->sessionEntity->status) || !in_array($this->sessionEntity->status, $this->statusList)){
			return false;
		}

		if (!is_null($status) && $status != $this->sessionEntity->status){
			return false;
		}

		return true;
	}

	/**
	 * Validate type of user
	 *
	 * @param string $userType: option
	 *
	 * @return bool
	 */
	public function validateUserType($userType = null)
	{
		// Check session entity

		if (is_null($this->sessionEntity)){
			return false;
		}

		// Validate type of user
		if (is_null($this->sessionEntity->userType) || !in_array($this->sessionEntity->userType, $this->roleList)){
			return false;
		}

		if (!is_null($userType) && $userType != $this->sessionEntity->userType){
			return false;
		}

		return true;
	}

	/**
	 * Check the role of the user whether his role is Admin or not
	 *
	 * @return bool true if user is Administrator else false
	 */
	public function isAdministrator($status = USER_STATUS_ACTIVE)
	{
		// Validate status of the user
		if ($this->validateStatus($status) == false){
			return false;
		}

		// Validate type of the user
		if ($this->validateUserType(ROLE_ADMIN) == false && $this->validateUserType(ROLE_ADMIN_SUPER) == false){
			return false;
		}

		return true;
	}

	/**
	 * Check the role of the user whether his role is Super Admin
	 *
	 * @return bool true if user is Administrator else false
	 */
	public function isSuperAdministrator($status = USER_STATUS_ACTIVE)
	{
		// Validate status of the user
		if ($this->validateStatus($status) == false){
			return false;
		}

		// Validate type of the user
		if ($this->validateUserType(ROLE_ADMIN_SUPER) == false){
			return false;
		}

		return true;
	}

	/**
	 * Check the role of the user whether his role is Member (Independent customer) or not
	 *
	 * @return bool true if user is Member (Independent customer) else false
	 */
	public function isMember($status = USER_STATUS_ACTIVE)
	{
		// Validate status of the user
		if ($this->validateStatus($status) == false){
			return false;
		}

		// Validate type of the user
		if ($this->validateUserType(ROLE_MEMBER) == false){
			return false;
		}

		return true;
	}

	public function isUserMember($status = USER_STATUS_ACTIVE)
	{
		// Validate status of the user
		if ($this->validateStatus($status) == false){
			return false;
		}

		// Validate type of the user
		if ($this->validateUserType(ROLE_MEMBER_USER) == false){
			return false;
		}

		return true;
	}

	/**
	 * Save session
	 *
	 * @return bool true if our system save session successfully else false
	 */
	public function saveSession($sessionEntity)
	{
		if (is_null($this->sessionEntity) || is_null($this->sessionEntity->id) || is_null($sessionEntity)){
			return false;
		}

		$this->sessionEntity = $sessionEntity;

		// Store SessionEntity
		$_SESSION[SESSION_USER_LOGGED] = $this->sessionEntity;

		// Return successfully
		return true;
	}

	/**
	 * Get id of the user currently logged in
	 *
	 * @return int id of the user
	 */
	public function getCurrentUserId()
	{
		if (is_null($this->sessionEntity) || is_null($this->sessionEntity->id)){
			return null;
		}

		// Return id of the user currently logged in
		return $this->sessionEntity->id;
	}

	/**
	 * Get session
	 *
	 * @return SessionEntity object
	 */
	public function getSession()
	{
		if (is_null($this->sessionEntity) || is_null($this->sessionEntity->id)){
			return null;
		}

		// Return SessionEntity
		return $this->sessionEntity;
	}

	/**
	 * Create a random password for user
	 *
	 * @param int $length : lenght of the password
	 *
	 * @return string : random passord has been created randomly
	 */
	public static function randomPassword($length)
	{
		srand(date("s"));
		$possible_charactors = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$string = "";
		while (strlen($string) < $length) {
			$string .= substr($possible_charactors, rand()%(strlen($possible_charactors)),1);
		}
		return $string;
	}

	/**
	 * Encrypt Password
	 *
	 * @param string $password : original password
	 *
	 * @return string password : password has been encrypted
	 */
	public static function encryptPassword($password)
	{
		// Return Password has been encrypt
		return md5(md5($password) . "secretepassword");
	}

	/**
	 * Get userType
	 *
	 * @return string userType
	 */
	public function getUserType(){
		return $this->sessionEntity->userType;
	}		
	
	/**
	 * Get CustoemrOrdersession
	 *
	 * @return CustomerOrderSessionEntity object
	 */
	public function getCustomerOrderSession()
	{
		if (is_null($this->customerOrderSessionEntity)){
			return null;
		}

		// Return SessionEntity
		return $this->customerOrderSessionEntity;
	}
	
	/**
	 * 
	 * Save Customer Order Session Entity
	 * @param CustomerOrderSessionEntity $customerOrderSessionEntity
	 */
	public function saveCustomerOrderSession($customerOrderSessionEntity)
	{
		if (is_null($this->customerOrderSessionEntity) || is_null($customerOrderSessionEntity)){
			return false;
		}

		$this->customerOrderSessionEntity = $customerOrderSessionEntity;

		// Store SessionEntity
		$_SESSION[SESSION_CUSTOMER_ORDER] = $this->customerOrderSessionEntity;

		// Return successfully
		return true;
	}
}
